package com.google.code.fluentjoiner.example;

import com.google.code.fluentjoiner.SerializableFunction;
import com.google.code.fluentjoiner.SupplierBuilder;
import com.google.code.fluentjoiner.io.http.HttpFileObjectSupplierBuilder;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.hazelcast.core.Hazelcast;
import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEnclosureImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.code.fluentjoiner.rss.RomeFunctions.*;
import static com.google.code.fluentjoiner.StringFunctions.*;

public class COOP
{
    private final static Logger LOGGER = LoggerFactory.getLogger(COOP.class);
    private final static String PAGE_REGEX = "http.*?--small.h264.mp4";

    public static void main(final String[] args) throws Exception
    {
        LOGGER.info("Starting COOP.main");
        final ExecutorService executorService = Hazelcast.getExecutorService();
        final SyndFeed feed = HttpFileObjectSupplierBuilder.fromPath("http://area5.tv/blog/rss.xml")
                .textFromFile()
                .transform(STRING_TO_SYNDFEED)
                .get();
        SupplierBuilder.fromValue(feed)
                .map(SYNDFEED_TO_ENTRIES)
                .parallelElementTransform(new SerializableFunction<SyndEntry, SyndEntry>()
                {
                    @Override
                    public SyndEntry apply(final SyndEntry syndEntry)
                    {
                        LOGGER.info("Looking up {}.", syndEntry.getLink());
                        final Set<String> pspVideoURLs = HttpFileObjectSupplierBuilder.fromPath(syndEntry.getLink())
                            .textFromFile()
                            .map(regexSearch(PAGE_REGEX))
                            .toSet()
                            .get();
                        if(Iterables.isEmpty(pspVideoURLs))
                        {
                            feed.getEntries().remove(syndEntry);
                        }
                        else
                        {
                            final String newLink = pspVideoURLs.iterator().next();
                            final SyndEnclosure enclosure = new SyndEnclosureImpl();
                            enclosure.setUrl(newLink);
                            final ArrayList<SyndEnclosure> enclosureList = Lists.newArrayList(enclosure);
                            syndEntry.setEnclosures(enclosureList);
                        }
                        return syndEntry;
                    }
                }, executorService)
                .get();

        final String feedText = SupplierBuilder.fromValue(feed).transform(SYNDFEED_TO_STRING).get();
        System.out.println(feedText);
        executorService.shutdown();
        Hazelcast.shutdown();
    }
}